package studentsystem1;

import javax.swing.*;
import javax.swing.table.JTableHeader;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.*;

public class NameQueryForm extends JFrame implements ActionListener
{
   JLabel labScore=new JLabel("请输入名字：");
  JTextField txtScore=new JTextField(10);
   JButton btnQuery=new JButton("查询");
   JPanel pan1=new JPanel();
   JPanel pan2=new JPanel();
   String []str={"姓名","学号","成绩"};
   Object[][] data=new Object[10][3];
   JTable table=new JTable(data,str);
   JTableHeader head=table.getTableHeader();
   JScrollPane jsp=new JScrollPane(table);        //滚动
  Connection con;
   Statement sql;
   ResultSet rs;
   NameQueryForm()
   {
       super("按名字查询");
       setSize(400,300);
       pan1.add(labScore);
       pan1.add(txtScore);
      pan1.add(btnQuery);                        //以上3条在第一个面板加入内容
      getContentPane().add(pan1,"North");        //把面板1放到窗口顶部
      pan2.setLayout(new BorderLayout());
       pan2.add(head,"North");            //将表头放在面板2的顶部
       pan2.add(jsp,"Center");            //表格放在pan2中间
       getContentPane().add(pan2,"Center");//把面板2整体加入到主窗口中
       btnQuery.addActionListener(this);  //增加查询按钮的监视器
       setVisible(true);
   }
   public void actionPerformed(ActionEvent ae){
       if(ae.getSource()==btnQuery){
           int i,j,row;

           try{
               Class.forName("com.mysql.cj.jdbc.Driver");}
           catch(ClassNotFoundException e){
               e.printStackTrace();
           }
           try{
               String url = "jdbc:mysql://localhost:3306/test?characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2B8";
               String user = "root";
               String passwd = "123456";
               con = DriverManager.getConnection(url, user, passwd);
               sql=con.createStatement();
               rs=sql.executeQuery("select COUNT(*) as rowcount from student where username='"+txtScore.getText()+"'");                   //寻找相同成绩的行数
               rs.next();                                        //去掉头
              row=rs.getInt("rowcount");                    
              rs.close();
               data=new Object[row][3];
               rs=sql.executeQuery("select * from student where username='"+txtScore.getText()+"'");
               i=0;j=0;
              while(rs.next()){
                   data[i][j++]=rs.getString("username");
                   data[i][j++]=rs.getInt("stu_time");
                   data[i][j]=new Integer(rs.getInt("stu_score"));//将数据库中查询到的信息保存到data中
                   i++;j=0;
               }
              pan2.removeAll();
              getContentPane().remove(pan2);
             table=new JTable(data,str);
               pan2.setLayout(new BorderLayout());
              head=table.getTableHeader();
               jsp=new JScrollPane(table);
               pan2.add(head,"North");
               pan2.add(jsp,"Center");
               getContentPane().add(pan2,"Center");
               this.validate();
               con.close();
           }catch(SQLException e){
               e.printStackTrace();
           }
       }
   }
   public static void main(String[] args) {
       new NameQueryForm();
  }
}